package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.*;
import io.swagger.models.auth.In;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 报价实体Controller
 * 
 * @author ruoyi
 * @date 2020-12-13
 */
@Controller
@RequestMapping("/system/offerinfo")
public class SysOfferinfoController extends BaseController
{
    private String prefix = "system/offerinfo";

    @Autowired
    private ISysOfferinfoService sysOfferinfoService;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private ISysPurchaseitemService sysPurchaseitemService;
    @Autowired
    private ISysGoodparameterService sysGoodparameterService;
    @Autowired
    private ISysBidinfoService sysBidinfoService;

    @RequiresPermissions("system:offerinfo:view")
    @GetMapping("/offer1")
    public String offerinfo()
    {
        return prefix + "/offerinfo";
    }
    @RequiresPermissions("system:offerinfo:view")
    @GetMapping("/offer2")
    public String offerinfo2()
    {
        return prefix + "/offerinfo2";
    }

    /**
     * 查询报价实体列表
     */
    @RequiresPermissions("system:offerinfo:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(JingJia jingJia)
    {
        SysOfferinfo sysOfferinfo = new SysOfferinfo();
        List<JingJia> list = new ArrayList<JingJia>();
        SysUser sysUser = ShiroUtils.getSysUser();
        if(sysUser.getUserType().equals("11")){
            /*SysSupplier supplier = new SysSupplier();
            supplier.setSupplierId(supplier.getSupplierId());*/
            startPage();
            list = sysOfferinfoService.searchBidInfo(sysUser.getCompanyId());

        }else if(sysUser.getUserType().equals("00")){
            startPage();
            list = sysOfferinfoService.searchBidInfo(null);
        }

        return getDataTable(list);
    }
    /**
     * 供应商报价历史查询
     * @param jingJia
     * @return
     */
    @RequiresPermissions("system:offerinfo:showBidInfoHistory")
    @PostMapping("/showBidInfoHistory")
    @ResponseBody
    public TableDataInfo showBidInfoHistory(JingJia jingJia){
        SysUser sysUser = ShiroUtils.getSysUser();
        List<JingJia> list = new ArrayList<JingJia>();
        if(sysUser.getUserType().equals("11")){
            /*startPage();*/
            list = sysOfferinfoService.offerInfoHistory(sysUser.getCompanyId());
        }
        return getDataTable(list);
    }
    /**
     * 导出报价实体列表
     */
    @RequiresPermissions("system:offerinfo:export")
    @Log(title = "报价实体", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysOfferinfo sysOfferinfo)
    {
        List<SysOfferinfo> list = sysOfferinfoService.selectSysOfferinfoList(sysOfferinfo);
        ExcelUtil<SysOfferinfo> util = new ExcelUtil<SysOfferinfo>(SysOfferinfo.class);
        return util.exportExcel(list, "offerinfo");
    }

    /**
     * 新增报价实体
     */
    @GetMapping("/addModel")
    public String add(Long bidInfoId, ModelMap mmap)
    {
        SysUser sysUser = ShiroUtils.getSysUser();
        mmap.put("bidInfoId",bidInfoId);
        SysBidinfo bidinfo=sysBidinfoService.selectSysBidinfoById(bidInfoId);
        mmap.put("count",bidinfo.getCount());
        mmap.put("demand",bidinfo.getDemand());
        SysPurchaseitem sysPurchaseitem=sysPurchaseitemService.selectSysPurchaseitemById(bidinfo.getPurchaseItemId());

        SysGoodparameter sysGoodparameter=new SysGoodparameter();
        sysGoodparameter.setGoodsId(sysPurchaseitem.getGoodsId());
        sysGoodparameter.setSupplierId(sysUser.getCompanyId());
        List<SysGoodparameter> sysGoodparameters=sysGoodparameterService.selectSysGoodparameterList(sysGoodparameter);
        mmap.put("sysGoodparameters",sysGoodparameters);
        return prefix + "/add";
    }

    /**
     * 新增保存报价实体
     */
//    @RequiresPermissions("system:offerinfo:add")
    @Log(title = "报价实体", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysOfferinfo sysOfferinfo)
    {
        Long unitId= ShiroUtils.getUserId();
        SysUser sysUser=userService.selectUserById(unitId);
        sysOfferinfo.setSupplierId(sysUser.getCompanyId());
        sysOfferinfo.setCreateTime(new Date());
        return toAjax(sysOfferinfoService.insertSysOfferinfo(sysOfferinfo));
    }

    /**
     * 修改报价实体
     */
    @GetMapping("/edit/{offerInfoId}")
    public String edit(@PathVariable("offerInfoId") Long offerInfoId, ModelMap mmap)
    {
        SysOfferinfo sysOfferinfo = sysOfferinfoService.selectSysOfferinfoById(offerInfoId);
        mmap.put("sysOfferinfo", sysOfferinfo);
        return prefix + "/edit";
    }

    /**
     * 修改保存报价实体
     */
    @RequiresPermissions("system:offerinfo:edit")
    @Log(title = "报价实体", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysOfferinfo sysOfferinfo)
    {
        return toAjax(sysOfferinfoService.updateSysOfferinfo(sysOfferinfo));
    }

    /**
     * 删除报价实体
     */
    @RequiresPermissions("system:offerinfo:remove")
    @Log(title = "报价实体", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysOfferinfoService.deleteSysOfferinfoByIds(ids));
    }
}
